//
//  BaseViewController.m
//  TrialRun
//
//  Created by Steven.Yung on 2017/3/7.
//  Copyright © 2017年 Minidai. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseNaviViewController.h"
#import "TTTAttributedLabel.h"
#import "MNDLoginUserModel.h"

@interface BaseViewController ()

@property (nonatomic, strong) UIColor *naviBarTitleDefaultColor;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewController:) name:@"popToRootViewControllerNotification" object:nil];
    //默认navigationBar的title为黑色
    self.naviBarTitleDefaultColor = [UIColor blackColor];
    
}



- (void)popToRootViewController:(NSNotification *)notification {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.naviBarTranslucent) {
        //导航条背景透明
        UIImage *image = [UIImage new];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        self.navigationController.navigationBar.shadowImage = image;
    }
    
    if (self.makeFullScreen) {
        self.automaticallyAdjustsScrollViewInsets = !self.makeFullScreen;
    }
    
    if (self.leftBarTintColor) {
        NSArray *leftBarButtonItems = self.navigationItem.leftBarButtonItems;
        for (UIBarButtonItem *item in leftBarButtonItems) {
            [item setTintColor:self.leftBarTintColor];
        }
    }
    if (self.barTitleColor) {
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: self.barTitleColor}];
    }
    NSInteger index = [self.navigationController.viewControllers indexOfObject:self];
    NSString *forgetGestureStr = [UserDefaultsTool objectForKey:kForgetGesture];
    if (index > 0 && ![MNDLoginUserModel isUserLogin] && [forgetGestureStr isEqualToString:@"YES"]) {
        [UserDefaultsTool saveObject:@"NO" forKey:kForgetGesture];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //如果设置了导航条背景透明，页面消失时要讲透明背景还原
    if (self.naviBarTranslucent) {
                //将透明背景图恢复成默认
        if ([self.navigationController isKindOfClass:[BaseNaviViewController class]]) {
            
            BaseNaviViewController *naviVC = (BaseNaviViewController *)self.navigationController;
            [naviVC configNavigationBarDefault];
        } else {
            [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        }
    }
    
    if (self.barTitleColor) {
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : self.naviBarTitleDefaultColor}];
    }
}

#pragma mark - Custom Method

- (TTTAttributedLabel *)labelWithText:(NSString *)text color:(UIColor *)color linkText:(NSString *)linkText linkColor:(UIColor *)linkColor {
    
    TTTAttributedLabel *linkLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    linkLabel.font = [UIFont systemFontOfSize:14.0];
    linkLabel.textColor = color;
    linkLabel.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber | NSTextCheckingTypeLink;
    linkLabel.linkAttributes = @{(id)kCTForegroundColorAttributeName: linkColor};
    [linkLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        
        NSRange range = [[mutableAttributedString string] rangeOfString:linkText options:NSCaseInsensitiveSearch];
        
        [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:linkColor range:range];
        
        return mutableAttributedString;
    }];
    NSRange range = [linkLabel.text rangeOfString:linkText];
    [linkLabel addLinkToPhoneNumber:linkText withRange:range];
    
    return linkLabel;
}

- (UIButton *)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)radius {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:title forState:UIControlStateNormal];

    [button setTitleColor:titleColor forState:UIControlStateNormal];
    
    [button.titleLabel setFont:font];
    
    [button setBackgroundColor:backgroundColor];
    
    button.layer.cornerRadius = radius;
    
    return button;
}

- (void)initBackButtonItemColor:(UIColor *)color {
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *itemImage = [UIImage imageNamed:@"navi_back_btn_black"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage: itemImage
                                                             style: UIBarButtonItemStylePlain
                                                            target: self
                                                            action: @selector(backAction:)];
    
    
    [item setTintColor:kColorBlack];
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = item;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, item];
    }
    for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setTintColor:color];
    }
}

- (void)backAction:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
